package com.cavalier.surtr.aspectj;

import com.cavalier.surtr.Exception.CheckBusinessException;
import com.cavalier.surtr.Exception.CheckParamException;
import com.cavalier.surtr.commonBase.ProcessResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by archangle on 17/2/18.
 */
@Aspect
@Component
public class ExceptionHandler {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(com.cavalier.surtr.commonBase.ProcessResult com.cavalier.surtr.impl.*..*Impl.*(..))")
    public void exceptionHandlerForCore() {
    }

    /**捕捉service实现上的异常*/
    @Around("exceptionHandlerForCore()")
    public Object doAroundForCore(ProceedingJoinPoint joinPoint) {
        log.info("ExceptionHandler 切面开始执行");
        try {
            return joinPoint.proceed();
        } catch (CheckParamException e) {
            log.error(e.getMessage());
            return ProcessResult.wrapErrorResult(e.getExceptionCode(), e.getMessage());
        } catch (CheckBusinessException e) {
            log.error(e.getMessage());
            return ProcessResult.wrapErrorResult(e.getExceptionCode(), e.getMessage());
        } catch (Throwable t) {
            log.error(t.getMessage());
            return ProcessResult.wrapErrorResult("00000011", t.getMessage());
        }
    }
}
